//
//  LGTBindWXVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/13.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import WisdomHUD

class LGTBindWXVC: LGTBaseVC {

    var loginInfo:Dictionary<String, Any>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.addNote()
    }
    
    func addNote(){
        NotificationCenter.default.addObserver(self, selector: #selector(bindWXAction), name: Bind_WX_Note, object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: Bind_WX_Note, object: nil)
    }
    
    @objc func bindWXAction(note:Notification){
        
        let code:String = note.userInfo?["code"] as? String ?? ""
        let data:Dictionary<String, Any> = self.loginInfo?["data"] as? Dictionary<String, Any> ?? [:]
        let user_id:Int = data["user_id"] as? Int ?? 0
        let user_token:String = data["user_token"] as? String ?? ""
        let param:Dictionary<String, Any> = ["code":code,
                                             "bind_user_id":user_id,
                                             "bind_user_token":user_token]
        LGTHttpUtil.httpUtil.requestData(url: Bind_WX_URL, param: param, success: { (json) in
            LGTViewUtil.share.loginAction(nav: self.navigationController, json: json, showMessage: false)
        }) { (json) in
            
        }
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "绑定微信", back: true)
        
        let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: CGFloat(15 * KRate), text: "为了保障账户安全，请先关联微信")
        self.view.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.height.equalTo(KRate * 15)
            make.top.equalTo(NavBarH() + 39 * KRate)
        }
        
        let appIcon = LGTViewCreateUtil.share.createImageView(image: "wx", contentMode: UIView.ContentMode.scaleAspectFill)
        self.view.addSubview(appIcon)
        appIcon.snp.makeConstraints { (make) in
            make.width.height.equalTo(KRate * 70)
            make.centerX.equalTo(self.view)
            make.top.equalTo(descLabel.snp_bottomMargin).offset(KRate * 55)
        }
        
        let wxLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: CGFloat(15 * KRate), text: "微信")
        self.view.addSubview(wxLabel)
        wxLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.height.equalTo(KRate * 15)
            make.top.equalTo(appIcon.snp_bottomMargin).offset(KRate * 15)
        }
        
        let bindBtn = LGTViewCreateUtil.share.createTextLGTButton(title: "关联微信", font: Font_Normal_15, color: UIColor.white, selectedColor: UIColor.white, corner:CGFloat(KRate * 20))
        bindBtn.backgroundColor = YellowColor
        self.view.addSubview(bindBtn)
        bindBtn.snp.makeConstraints { (make) in
            make.left.equalTo(KRate * 75)
            make.right.equalTo(KRate * -75)
            make.height.equalTo(40 * KRate)
            make.top.equalTo(wxLabel.snp_bottomMargin).offset(KRate * 50)
        }
        
        bindBtn.addTarget(self, action: #selector(bindAction), for: UIControl.Event.touchUpInside)
    }
    
    
    
    @objc func bindAction(){
        //判断是否安装微信
        if WXApi.isWXAppInstalled() == false{
            WisdomHUD.showError(text: "请安装微信")
            return
        }
        
        LGTUserUtil.share.bindWXState = true
        let req = SendAuthReq.init()
        req.scope = "snsapi_userinfo"
        req.state = "123"
        WXApi.send(req)
        
        self.view.isUserInteractionEnabled = false
        weak var weakSelf = self
        let deadline = DispatchTime.now() + 1.0
        DispatchQueue.global().asyncAfter(deadline: deadline, execute: DispatchWorkItem.init(block: {
            weakSelf?.view.isUserInteractionEnabled = true
        }))
    }
}
